package com.atguigu.gmall.product.controller;

import java.util.concurrent.*;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author atguigu-mqx
 */
public class TestComPare {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建一个无返回值的对象
        //        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
        //            System.out.println("无返回值..");
        //        });
        //
        //        //  计算完成时回调方法
        //        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
        //            //  int i = 1/0;
        //            return 1024;
        //        }).thenApply(new Function<Integer, Integer>() {
        //            @Override
        //            public Integer apply(Integer integer) {
        //                System.out.println("thenApply:\t"+integer);
        //                return integer*2;
        //            }
        //        }).whenComplete(new BiConsumer<Integer, Throwable>() {
        //            @Override
        //            public void accept(Integer integer, Throwable throwable) {
        //                System.out.println("integer"+integer);
        //                System.out.println("throwable"+throwable);
        //            }
        //        }).exceptionally((t)->{
        //            System.out.println("t:\t"+t);
        //            return 404;
        //        });
        //        //  System.out.println(completableFuture.get());
        //        System.out.println(future.get());

        //  创建线程池！
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5,
                100,
                3,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3)
                //                Executors.defaultThreadFactory(),
                //                new ThreadPoolExecutor.AbortPolicy()
        );

        //  创建一个A线程：返回hello ！
        CompletableFuture<String> futureA = CompletableFuture.supplyAsync(() -> {
            return "hello";
        },threadPoolExecutor);

        //   创建一个B线程：获取hello ！
        CompletableFuture<Void> futureB = futureA.thenAcceptAsync((s) -> {
            //  睡一会！
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //  打印数据
            System.out.println(s + ":\tB");
        },threadPoolExecutor);

        //   创建一个C线程：获取hello ！
        CompletableFuture<Void> futureC = futureA.thenAcceptAsync((s) -> {
            //  睡一会！
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //  打印数据
            System.out.println(s + ":\tC");
        },threadPoolExecutor);

        System.out.println(futureA.get());
        System.out.println(futureB.get());
        System.out.println(futureC.get());

        //  释放资源.....
        threadPoolExecutor.shutdown();


    }
}
